package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.PartySignAddDTO;
import org.jsola.contract.dto.PartySignBatchDTO;
import org.jsola.contract.dto.PartySignUpdateDTO;
import org.jsola.contract.query.PartySignQuery;
import org.jsola.contract.service.IPartySignService;
import org.jsola.contract.vo.PartySignListVO;
import org.jsola.contract.vo.PartySignVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同签署方签署记录
 *
 * @author JanusMix
 */
@RestController("contractPartySignController")
@RequestMapping(ContractConstants.API_PATH + "/v1/partySigns")
public class PartySignController implements IPartySignControllerAPI {

    @Autowired
    private IPartySignService partySignService;

    @Override
    @PostMapping
    public Result<PartySignVO> save(@Valid @RequestBody PartySignAddDTO partySignAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同签署方签署记录权限校验

        return Result.success(
                partySignService.save(partySignAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PartySignUpdateDTO partySignUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同签署方签署记录权限校验

        int count = partySignService.update(partySignUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody PartySignBatchDTO partySignBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<PartySignAddDTO> partySignAddDTOList = partySignBatchDTO.getPartySignAddDTOList();
        if (!CollectionUtils.isEmpty(partySignAddDTOList)) {
            partySignService.batchSave(partySignAddDTOList,tokenUser);
        }
        // 修改
        List<PartySignUpdateDTO> partySignUpdateDTOList = partySignBatchDTO.getPartySignUpdateDTOList();
        if (!CollectionUtils.isEmpty(partySignUpdateDTOList)) {
            partySignUpdateDTOList.parallelStream().forEach(
                    partySignUpdateDTO -> {
                        partySignService.update(partySignUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deletePartySignIdList = partySignBatchDTO.getDeletePartySignIdList();
        if (!CollectionUtils.isEmpty(deletePartySignIdList)) {
            partySignService.deleteByIds(tokenUser,deletePartySignIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{partySignId}")
    public Result<Integer> delete(@PathVariable Long partySignId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同签署方签署记录权限校验

        int count = partySignService.deleteByIds(tokenUser, partySignId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] partySignIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同签署方签署记录权限校验

        int count = partySignService.deleteByIds(tokenUser, partySignIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{partySignId}")
    public Result<PartySignVO> findById(@PathVariable Long partySignId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同签署方签署记录权限校验

        return Result.success(
            partySignService.selectById(partySignId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PartySignListVO>> listAll(@Valid PartySignQuery partySignQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同签署方签署记录权限校验

        return Result.success(
            partySignService.select(partySignQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<PartySignListVO>> page(@Valid PartySignQuery partySignQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            partySignService.selectPage(partySignQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/listAll/{accountId}")
    public Result<List<PartySignListVO>> listAllByAccountId(@PathVariable String accountId) {

        List<PartySignListVO> partySignListVOList = partySignService.listAllByAccountId(accountId);
        if (!CollectionUtils.isEmpty(partySignListVOList)) {
            partySignListVOList.forEach(partySignListVO -> partySignService.setContractInfo(partySignListVO));
        }
        return Result.success(
                partySignListVOList
        );
    }


}

